#!/usr/bin/perl -w

#use strict;
use Data::Dumper;
use HTML::HashTable;
use DBI;

my %except_num;
my %warn;
my %sid;
my %except_value;
my @logfile=<./192.168.224.*>;
my @func;
my @result;


#############################################
## set regex for match
######
my $regex=qr/.*sid=(\w+).*,result=(\w+_\w+(?:(?:_\w+)+)?),.*/;
my $nregex=qr/.*WARNING \[tid:\d+\](.*)! func=.*,sid=(\w+),.*/;

my %a;

############################################
## Got data from file
###
for my $logfile (@logfile)
{

###############################################################  
## Got result data except result=S_OK and save into hash table
###
open (FH,"<",$logfile) or die $!;

while (<FH>)
{
	next if (/S_PARTIAL_OK/);
	if (/$regex/) 
	{
	$sid{$1}++;
	$except_num{$2}{"num"}++;
	push @{$except_num{$2}{"sid"}},$1;
	
	}
}
close(FH);
###############################################################
## Got detail data which match result we got in previous
###

open (NFH,"<",$logfile) or die $!;
while (<NFH>) 
{
	if (/$nregex/)
	{	
		# check if match sid which has been got in previous step
		if (exists $sid{$2})
		{
		$warn{$1}++;
			# get detail data which line match FS_UNKNOW
			if (/FS_UNKNOW/)
			{
			# get func numbers
			$a{$1}{'num'}++ if (/.*func=(.*:\w+),.*/);
			my $c=$1;
			my $d=grep /$c/,@func;
			push @func,$c if ($d eq "0");
			# get excption values 
			if (/.*exception=(.*)com.*/)
				{
				my $b=$1;
				$b=~s/in string.*/in string/g;
				$b=~s/data!.*/data!/g;
				$b=~s/completed:.*/completed/g;
				$b=~s/for host.*//g;
		#		$a{'FS_UNKNOW'}{exception}{$b}++;
				$a{$c}{'exception'}{$b}++;
				my $i=0;
				$except_value{$c}[$i]=$b if not exists $except_value{$c};
				next if ("$except_value{$c}[$i]" eq "$b");
					if ((exists $except_value{$c}[$i]) and ("$except_value{$c}[$i]" ne "$b"))
					{
					$i++;
					$except_value{$c}[$i]=$b;
					}
	#			next if grep /$b/,@result;
	#			push @result,$b;
				}
#			print $_,"\n";
			}
		} 
	}
}
close (NFH);
}
#print Dumper \@func;
#print Dumper \%warn;
#print Dumper \%except_num;
#print Dumper \%sid;
#print Dumper \%a;
#print Dumper \%except_value;

#print tablify({
#	BORDER => 0,
#	DATA  => \%a,
#	SORTBY => 'key',
#	ORDER => 'asc'}
#);


#for my $line (keys %except_num)
#{	
#	print "$except_num{$line}{'num'}\t$line\n";
#}
#for my $func (@func)
#{
#	 (@result)
#	{
#	print "$a{'FS_UNKNOW'}{$func}{'num'}\t$func\n";
#	}
#}

#sub sqlite {

# insert data into SQLite 
my $dbh=DBI->connect("dbi:SQLite:dbname=a.f","","",{AutoCommit=>0});
my $sql="create table aaa ( key,num1,value,num2 )";
$dbh->do( $sql );

#$sql="insert into aaa values ('$key','$num1','$value','$num2')";
$sql="insert into aaa values (?, ?, ?, ?)"; 
my $sth = $dbh->prepare($sql); # use prepare() to avoid special characters 
while (my ($func,$value)=each %except_value)
{
	my $key=$func;
	my $num=$a{$func}{'num'};
        for (my $i=0;$i<=$#{$value};$i++)
        {
	my $value=@$value[$i];
	my $num2=$a{"$func"}{'exception'}{"$value"};
	#$key=$dbh->quote($key) if $key =~/JSP/;
	#$value=$dbh->quote($value) if $value=~/req|ouid|Missing/;
	#print "$key\n";
	#print "$value\n";
	$sth->execute($key,$num,$value,$num2);
#	$dbh->do( $sql );
        }
}
$sth->finish;
#$sql="insert into aaa values ('$key','$num1','$value','$num2')";
#$dbh->do( $sql );
$sql="select * from aaa";
my $dbconn=$dbh->prepare($sql);
$dbconn->execute();
$dbconn->dump_results();
my $row_ary=$dbconn->fetchall_arrayref();
$dbconn->finish();
$dbh->disconnect();
#print Dumper \%a;
#}
